
#The syntax that follows replicates the analyses included in Carbone, Harell and Soroka,
#"Critical Race Theory: How Policy Language Differentially Engages Symbolic Racism and Partisanship"

#This file is distributed with the dataset Carbone.Harell.Soroka.data.Rdata
#Variables in the dataset are as follows:

#pid - Party id, 7-pt scale
#pid1 - Party ID, rescaled from 0-1
#pid3 - Party ID, 3 categories
#wid - White Identity Scale
#resentment - Symbolic Racism Scale
#CRT.scale - CRT Scale
#crt.trt - Experimental Treatment
#female - Sex
#Age - age
#Education_cat - Education
#Q101_1 - Race: Arab
#Q101_2 - Race: Asian
#Q101_3 - Race: Black or African American  
#Q101_4 - Race: American Indian or Alaska Native
#Q101_5 - Race: Latino 
#Q101_8 - Race: White
#Q101_9 - Race: None of these

library(dplyr)
library(modelsummary)
library(effects)
library(car)
library(haven)
load("Carbone.Harell.Soroka.data.Rdata")


#########################
#Tables


#Table 1.

model1 <- lm(CRT.scale ~ crt.trt, data=a)
summary(model1)
cm <- c("crt.trt2.disc" = "Treatment: Disc",
        "crt.trt3.white" = "Treatment: White",
        "crt.trt4.crt" = "Treatment: CRT",
        "(Intercept)" = "Intercept")
modelsummary(model1, stars=T, title="Table 1: Policy Support Across Treatments",
             gof_omit="IC|RMSE|F|Adj",
             coef_map=cm)
linearHypothesis(model1,"crt.trt2.disc = crt.trt3.white")
linearHypothesis(model1,"crt.trt2.disc = crt.trt4.crt")
linearHypothesis(model1,"crt.trt3.white = crt.trt4.crt")


#Table 2.

model2 <- lm(CRT.scale ~ crt.trt + resentment, data=a)
model3 <- lm(CRT.scale ~ crt.trt + pid1, data=a)
model4 <- lm(CRT.scale ~ crt.trt + wid, data=a)
model5 <- lm(CRT.scale ~ crt.trt + resentment + pid1 + wid, data=a)
cm <- c("crt.trt2.disc" = "Treatment: Disc",
        "crt.trt3.white" = "Treatment: White",
        "crt.trt4.crt" = "Treatment: CRT",
        "resentment" = "SR",
        "pid1" = "Party ID",
        "wid" = "White Identity",
        "(Intercept)" = "Intercept")
modelsummary(list(model2, model3, model4, model5),
             stars=T, title="Table 2. The Effects of Discrimination, PID and WID on Policy Support",
             gof_omit="IC|RMSE|F|Adj",
             coef_map=cm)


#Table 3.
model6 <- lm(CRT.scale ~ crt.trt * pid1, data=a)
model7 <- lm(CRT.scale ~ crt.trt * wid, data=a)
cm <- c("crt.trt2.disc" = "Treatment: Disc",
        "crt.trt3.white" = "Treatment: White",
        "crt.trt4.crt" = "Treatment: CRT",
        "pid1" = "Party ID",
        "crt.trt2.disc:pid1" = "Party ID * Treatment: Disc",
        "crt.trt3.white:pid1" = "Party ID * Treatment: White",
        "crt.trt4.crt:pid1" = "Party ID * Treatment: CRT",
        "wid" = "White Identity",
        "crt.trt2.disc:wid" = "White Identity * Treatment: Disc",
        "crt.trt3.white:wid" = "White Identity * Treatment: White",
        "crt.trt4.crt:wid" = "White Identity * Treatment: CRT",
        "(Intercept)" = "Intercept")
modelsummary(list(model6, model7),
             stars=T, title="Table 3. The Varying Impact of PID and WID Across Treatments",
             gof_omit="IC|RMSE|F|Adj",
             coef_map=cm)
linearHypothesis(model6,"crt.trt2.disc = crt.trt4.crt")
linearHypothesis(model6,"crt.trt3.white = crt.trt4.crt")
linearHypothesis(model6,"crt.trt2.disc:pid1 = crt.trt3.white:pid1")
linearHypothesis(model6,"crt.trt2.disc:pid1 = crt.trt4.crt:pid1")
linearHypothesis(model6,"crt.trt3.white:pid1 = crt.trt4.crt:pid1")
linearHypothesis(model7,"crt.trt2.disc:wid = crt.trt3.white:wid")
linearHypothesis(model7,"crt.trt2.disc:wid = crt.trt4.crt:wid")
linearHypothesis(model7,"crt.trt3.white:wid = crt.trt4.crt:wid")


#Appendix Table 1.
table(a$female)
table(a$Age)
table(a$Education_cat)
table(a$Q101_1)
table(a$Q101_2)
table(a$Q101_3)
table(a$Q101_4)
table(a$Q101_5)
table(a$Q101_8)
table(a$Q101_9)
table(a$Q101_8)


#Appendix Table 2.
model6a <- lm(CRT.scale ~ crt.trt * resentment, data=a[a$pid3=="D",])
model6b <- lm(CRT.scale ~ crt.trt * resentment, data=a[a$pid3=="I",])
model6c <- lm(CRT.scale ~ crt.trt * resentment, data=a[a$pid3=="R",])
cm <- c("crt.trt2.disc" = "Treatment: Disc",
        "crt.trt3.white" = "Treatment: White",
        "crt.trt4.crt" = "Treatment: CRT",
        "resentment" = "Resentment",
        "crt.trt2.disc:resentment" = "Resentment * Treatment: Disc",
        "crt.trt3.white:resentment" = "Resentment * Treatment: White",
        "crt.trt4.crt:resentment" = "Resentment * Treatment: CRT",
        "(Intercept)" = "Intercept")
modelsummary(list(model6a, model6b, model6c),
             stars=T, title="Table 4. The Impact of PID, by Levels of Resentment",
             gof_omit="IC|RMSE|F|Adj",
             coef_map=cm)


#Appendix Table 3.
model7a <- lm(CRT.scale ~ crt.trt * wid, data=a[a$pid3=="D",])
model7b <- lm(CRT.scale ~ crt.trt * wid, data=a[a$pid3=="I",])
model7c <- lm(CRT.scale ~ crt.trt * wid, data=a[a$pid3=="R",])
cm <- c("crt.trt2.disc" = "Treatment: Disc",
        "crt.trt3.white" = "Treatment: White",
        "crt.trt4.crt" = "Treatment: CRT",
        "wid" = "Resentment",
        "crt.trt2.disc:wid" = "WID * Treatment: Disc",
        "crt.trt3.white:wid" = "WID * Treatment: White",
        "crt.trt4.crt:wid" = "WID * Treatment: CRT",
        "(Intercept)" = "Intercept")
modelsummary(list(model7a, model7b, model7c),
             stars=T, title="Table 4. The Impact of PID, by Levels of Resentment",
             gof_omit="IC|RMSE|F|Adj",
             coef_map=cm)


#########################
#Figures


#Figure 1.
pid1 <- a[a$pid<3,]
pid4 <- a[a$pid==3,]
pid7 <- a[a$pid>3,]
{
  pdf("figure1.pdf",width=6,height=6)
  plot(jitter(pid1$wid,1.5),jitter(pid1$resentment,1.5),type="n",axes=F, ann=F, ylim=c(0,1), xlim=c(0,1))
  set.seed(11) ; points(jitter(pid1$wid,1.5),jitter(pid1$resentment,1.5),col="dodgerblue3")
  set.seed(21) ; points(jitter(pid4$wid,1.5),jitter(pid4$resentment,1.5),col="black")
  set.seed(31) ; points(jitter(pid7$wid,1.5),jitter(pid7$resentment,1.5),col="firebrick3")
  axis(1)
  axis(2, las=1)
  abline(h=.5,lwd=1,lty=2)
  abline(v=.5,lwd=1,lty=2)
  mtext("White Identity",side=1, line=2.5)
  mtext("Symbolic Racism",side=2, line=3)
  text(1,.08,"Republican Identifers", col="firebrick3", cex=.8, pos=2)
  text(1,.04,"Democratic Identifers", col="dodgerblue3", cex=.8, pos=2)
  text(1,0,"Independents", col="black", cex=.8, pos=2)
  invisible(dev.off())
}
C <- a[,c("resentment","wid","pid")]
round(cor(C,use="complete.obs"),3)


#Figure 2.
eff <- effect("crt.trt",model1)
effr <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr$order <- c(1:4)
{
  pdf("figure2.pdf",width=6,height=6)
  plot(effr$order,effr$fit,ann=F,axes=F, ylim=c(.4,.8), xlim=c(.5,4.5), type="n")
  arrows(effr$order,effr$lower,effr$order,effr$higher,length=.1,angle=90, col="gray", code=3)
  points(effr$order,effr$fit,pch=15, cex=1.5)
  axis(1, col="white",at=c(1,2,3,4),labels=c("History","Discrimination","Privilege","CRT"))
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(.6,.45,"Squares indicate predicted values,\nwhiskers indicate 95% confidence intervals.", pos=4, cex=.8)
  invisible(dev.off())
}


#Figure 3.
eff <- effect("crt.trt * pid1", model6)
effr <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr <- effr[order(effr$crt.trt,effr$pid1),]
effr$order <- as.numeric(substr(effr$crt.trt,1,2))
{
  pdf("figure3.pdf",width=6,height=6)
  plot(effr$order,effr$fit,ann=F,axes=F, ylim=c(.2,.9), xlim=c(.5,4.5), type="n")
  arrows(effr$order[effr$pid1==0],effr$lower[effr$pid1==0],effr$order[effr$pid1==0],effr$higher[effr$pid1==0],length=.1,angle=90, col="gray", code=3)
  arrows(effr$order[effr$pid1==1],effr$lower[effr$pid1==1],effr$order[effr$pid1==1],effr$higher[effr$pid1==1],length=.1,angle=90, col="gray", code=3)
  points(effr$order[effr$pid1==0], effr$fit[effr$pid1==0],col="dodgerblue3",pch=15, cex=1.5)
  points(effr$order[effr$pid1==1], effr$fit[effr$pid1==1],col="firebrick3",pch=15, cex=1.5)
  axis(1, col="white",at=c(1,2,3,4),labels=c("History","Discrimination","Privilege","CRT"))
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(.6,.23,"Squares indicate predicted values,\nwhiskers indicate 95% confidence intervals.\nDemocrats in blue and Republicans in red.", pos=4, cex=.8)
  invisible(dev.off())
}


#Figure 4.
eff <- effect("crt.trt * resentment", model6a, xlevels=list(resentment=c(.1,.2,.3,.4,.5,.6,.7,.8,.9,1)))
effrD <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effrD <- effrD[order(effrD$crt.trt,effrD$resentment),]
effrD$order <- as.numeric(substr(effrD$crt.trt,1,2))
eff <- effect("crt.trt * resentment", model6c, xlevels=list(resentment=c(.1,.2,.3,.4,.5,.6,.7,.8,.9,1)))
effrR <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effrR <- effrR[order(effrR$crt.trt,effrR$resentment),]
effrR$order <- as.numeric(substr(effrR$crt.trt,1,2))

##Figure 4: CRT
poly <- c(effrD$resentment[effrD$crt.trt=="4.crt"],rev(effrD$resentment[effrD$crt.trt=="4.crt"]))
gon <- c(effrD$lower[effrD$crt.trt=="4.crt"],rev(effrD$higher[effrD$crt.trt=="4.crt"]))
polygD <- as.data.frame(cbind(poly,gon))
poly <- c(effrR$resentment[effrD$crt.trt=="4.crt"],rev(effrR$resentment[effrD$crt.trt=="4.crt"]))
gon <- c(effrR$lower[effrD$crt.trt=="4.crt"],rev(effrR$higher[effrD$crt.trt=="4.crt"]))
polygR <- as.data.frame(cbind(poly,gon))
{
  pdf("figure4.crt.pdf",width=6,height=6)
  plot(effrD$resentment,effrD$fit,ann=F,axes=F, ylim=c(0,1.2), xlim=c(0,1), type="n")
  polygon(polygD$poly,polygD$gon,border=NA,col="dodgerblue3",density=36,angle=90)
  polygon(polygR$poly,polygR$gon,border=NA,col="firebrick3",density=36,angle=90)
  lines(effrD$resentment[effrD$crt.trt=="4.crt"], effrD$fit[effrD$crt.trt=="4.crt"],col="dodgerblue3", lwd=2)
  lines(effrR$resentment[effrR$crt.trt=="4.crt"], effrR$fit[effrR$crt.trt=="4.crt"],col="firebrick3", lwd=2)
  axis(1)
  mtext("Symbolic Racism", side=1, line=3)
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(1,1.1,"CRT Treatment",pos=2, cex=1.2)
  invisible(dev.off())
}

##Figure 4: Hist
poly <- c(effrD$resentment[effrD$crt.trt=="1.hist"],rev(effrD$resentment[effrD$crt.trt=="1.hist"]))
gon <- c(effrD$lower[effrD$crt.trt=="1.hist"],rev(effrD$higher[effrD$crt.trt=="1.hist"]))
polygD <- as.data.frame(cbind(poly,gon))
poly <- c(effrR$resentment[effrD$crt.trt=="1.hist"],rev(effrR$resentment[effrD$crt.trt=="1.hist"]))
gon <- c(effrR$lower[effrD$crt.trt=="1.hist"],rev(effrR$higher[effrD$crt.trt=="1.hist"]))
polygR <- as.data.frame(cbind(poly,gon))
{
  pdf("figure4.hist.pdf",width=6,height=6)
  plot(effrD$resentment,effrD$fit,ann=F,axes=F, ylim=c(0,1.2), xlim=c(0,1), type="n")
  polygon(polygD$poly,polygD$gon,border=NA,col="dodgerblue3",density=36,angle=90)
  polygon(polygR$poly,polygR$gon,border=NA,col="firebrick3",density=36,angle=90)
  lines(effrD$resentment[effrD$crt.trt=="1.hist"], effrD$fit[effrD$crt.trt=="1.hist"],col="dodgerblue3", lwd=2)
  lines(effrR$resentment[effrR$crt.trt=="1.hist"], effrR$fit[effrR$crt.trt=="1.hist"],col="firebrick3", lwd=2)
  axis(1)
  mtext("Symbolic Racism", side=1, line=3)
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(0,.2,"Lines show predicted values\nwhiskers indicate 95% confidence intervals.\nDemocrats in blue and Republicans in red.", pos=4, cex=.8)
  text(1,1.1,"History Treatment",pos=2, cex=1.2)
  invisible(dev.off())
}

##Figure 4: Disc
poly <- c(effrD$resentment[effrD$crt.trt=="2.disc"],rev(effrD$resentment[effrD$crt.trt=="2.disc"]))
gon <- c(effrD$lower[effrD$crt.trt=="2.disc"],rev(effrD$higher[effrD$crt.trt=="2.disc"]))
polygD <- as.data.frame(cbind(poly,gon))
poly <- c(effrR$resentment[effrD$crt.trt=="2.disc"],rev(effrR$resentment[effrD$crt.trt=="2.disc"]))
gon <- c(effrR$lower[effrD$crt.trt=="2.disc"],rev(effrR$higher[effrD$crt.trt=="2.disc"]))
polygR <- as.data.frame(cbind(poly,gon))
{
  pdf("figure4.disc.pdf",width=6,height=6)
  plot(effrD$resentment,effrD$fit,ann=F,axes=F, ylim=c(0,1.2), xlim=c(0,1), type="n")
  polygon(polygD$poly,polygD$gon,border=NA,col="dodgerblue3",density=36,angle=90)
  polygon(polygR$poly,polygR$gon,border=NA,col="firebrick3",density=36,angle=90)
  lines(effrD$resentment[effrD$crt.trt=="2.disc"], effrD$fit[effrD$crt.trt=="2.disc"],col="dodgerblue3", lwd=2)
  lines(effrR$resentment[effrR$crt.trt=="2.disc"], effrR$fit[effrR$crt.trt=="2.disc"],col="firebrick3", lwd=2)
  axis(1)
  mtext("Symbolic Racism", side=1, line=3)
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(1,1.1,"Discrimination Treatment",pos=2, cex=1.2)
  invisible(dev.off())
}

##Figure 4: WID
poly <- c(effrD$resentment[effrD$crt.trt=="3.white"],rev(effrD$resentment[effrD$crt.trt=="3.white"]))
gon <- c(effrD$lower[effrD$crt.trt=="3.white"],rev(effrD$higher[effrD$crt.trt=="3.white"]))
polygD <- as.data.frame(cbind(poly,gon))
poly <- c(effrR$resentment[effrD$crt.trt=="3.white"],rev(effrR$resentment[effrD$crt.trt=="3.white"]))
gon <- c(effrR$lower[effrD$crt.trt=="3.white"],rev(effrR$higher[effrD$crt.trt=="3.white"]))
polygR <- as.data.frame(cbind(poly,gon))
{
  pdf("figure4.white.pdf",width=6,height=6)
  plot(effrD$resentment,effrD$fit,ann=F,axes=F, ylim=c(0,1.2), xlim=c(0,1), type="n")
  polygon(polygD$poly,polygD$gon,border=NA,col="dodgerblue3",density=36,angle=90)
  polygon(polygR$poly,polygR$gon,border=NA,col="firebrick3",density=36,angle=90)
  lines(effrD$resentment[effrD$crt.trt=="3.white"], effrD$fit[effrD$crt.trt=="3.white"],col="dodgerblue3", lwd=2)
  lines(effrR$resentment[effrR$crt.trt=="3.white"], effrR$fit[effrR$crt.trt=="3.white"],col="firebrick3", lwd=2)
  axis(1)
  mtext("Symbolic Racism", side=1, line=3)
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  text(1,1.1,"Privilege Treatment",pos=2, cex=1.2)
  invisible(dev.off())
}


#Appendix Figure 1.
barplot(table(a$CRT.scale), las=1)
barplot(table(a$resentment), las=1)
barplot(table(a$pid1), las=1, names.arg=c("0",".17",".33",".5",".66",".83","1"))
barplot(table(a$wid), las=1)


#Appendix Figure 2.
eff <- effect("crt.trt * wid", model7)
effr <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr <- effr[order(effr$crt.trt,effr$wid),]
effr$order <- as.numeric(substr(effr$crt.trt,1,2))
{
  pdf("appendixfigure2.pdf",width=6,height=6)
  plot(effr$order,effr$fit,ann=F,axes=F, ylim=c(.2,.9), xlim=c(.5,4.5), type="n")
  arrows(effr$order[effr$wid==.05],effr$lower[effr$wid==.05],effr$order[effr$wid==.05],effr$higher[effr$wid==.05],length=.1,angle=90, col="gray", code=3)
  arrows(effr$order[effr$wid==1],effr$lower[effr$wid==1],effr$order[effr$wid==1],effr$higher[effr$wid==1],length=.1,angle=90, col="gray", code=3)
  points(effr$order[effr$wid==.05], effr$fit[effr$wid==.05],col="dodgerblue3",pch=15, cex=1.5)
  points(effr$order[effr$wid==1], effr$fit[effr$wid==1],col="firebrick3",pch=15, cex=1.5)
  axis(1, col="white",at=c(1,2,3,4),labels=c("History","Discrimination","Privilege","CRT"))
  axis(2, las=1)
  mtext("Support for Policy", side=2, line=3)
  invisible(dev.off())
}


